package com.wuhunyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuhunyu.client.CommentServiceClient;
import com.wuhunyu.commonutils.check.Check;
import com.wuhunyu.commonutils.jwt.JwtUtils;
import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.entities.EduComment;
import com.wuhunyu.mapper.EduCommentMapper;
import com.wuhunyu.mapper.EduCourseMapper;
import com.wuhunyu.mapper.EduTeacherMapper;
import com.wuhunyu.query.EduCommentQuery;
import com.wuhunyu.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuhunyu.servicebase.exception.Assert;
import com.wuhunyu.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author wuhunyu
 * @since 2021-01-25
 */
@Service("eduCommentService")
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Resource
    private EduCommentMapper eduCommentMapper;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Resource
    private EduTeacherMapper eduTeacherMapper;

    @Autowired
    private CommentServiceClient commentServiceClient;

    @Override
    public CommonResult selectAllComments(EduCommentQuery eduCommentQuery) {
        // 校验参数
        Assert.isRight(Check.isBlank(eduCommentQuery), "评论查询参数不能为空");
        Assert.isRight(Check.isBlank(eduCommentQuery.getCourseId()), "课程id不能为空");
        Assert.isRight(Check.isBlank(eduCommentQuery.getTeacherId()), "讲师id不能为空");
        // 查询课程是否存在
        Assert.isRight(Check.isBlank(eduCourseMapper.selectById(eduCommentQuery.getCourseId())), "课程不存在");
        // 讲师是否存在
        Assert.isRight(Check.isBlank(eduTeacherMapper.selectById(eduCommentQuery.getTeacherId())), "讲师不存在");
        // 执行查询操作
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", eduCommentQuery.getCourseId());
        queryWrapper.eq("teacher_id", eduCommentQuery.getTeacherId());
        // 按时间倒叙
        queryWrapper.orderByDesc("gmt_create");
        Page<EduComment> page = new Page<>(eduCommentQuery.getCurrent(), eduCommentQuery.getLimit());
        Map<String, Object> map = new PageUtil<EduComment>().generateMap(eduCommentMapper.selectPage(page, queryWrapper));
        // 封装结果
        return CommonResult.success("评论信息查询成功", map);
    }

    @Override
    public CommonResult selectCommentByMemberId(String memberId) {
        // 校验参数
        Assert.isRight(Check.isBlank(memberId), "用户id不能为空");
        // 执行查询操作
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        List<EduComment> comments = eduCommentMapper.selectList(queryWrapper);
        Assert.isRight(comments == null || comments.size() == 0, "查询结果为空");
        // 封装结果集
        Map<String, Object> map = new HashMap<>(16);
        map.put("comments", comments);
        return CommonResult.success("评论信息查询成功", map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult addComment(EduComment eduComment, HttpServletRequest request) {
        // 参数校验
        Assert.isRight(Check.isBlank(eduComment), "新增评论参数不能为空");
        // 课程id
        Assert.isRight(Check.isBlank(eduComment.getCourseId()), "课程不能为空");
        // 讲师id
        Assert.isRight(Check.isBlank(eduComment.getTeacherId()), "讲师id不能为空");
        // 评论内容
        Assert.isRight(Check.isBlank(eduComment.getContent()), "评论不能为空");
        // 用户信息
        // 通过request获取用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        Assert.isRight(Check.isBlank(id), "用户尚未登录");
        Map<String, Object> map = (HashMap<String, Object>) commentServiceClient.selectMemberInfo(id).getData().get("info");
        Assert.isRight(Check.isBlank(map), "调用用户接口失败");
        // 由于是调用自己的接口，数据来源是数据库，数据为非空则为可信
        Assert.isRight(Check.isBlank(map.get("id")), "用户id不能为空");
        Assert.isRight(Check.isBlank(map.get("nickname")), "用户昵称不能为空");
        Assert.isRight(Check.isBlank(map.get("avatar")), "用户头像不能为空");
        eduComment.setMemberId((String) map.get("id"));
        eduComment.setNickname((String) map.get("nickname"));
        eduComment.setAvatar((String) map.get("avatar"));
        // 执行插入操作
        Assert.isRight(eduCommentMapper.insert(eduComment) != 1, "评论新增失败");
        return CommonResult.success("评论新增成功");
    }

    /**
     * 保留
     * @param eduComment
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateComment(EduComment eduComment) {
        return null;
    }

    /**
     * 保留
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult deleteCommentById(String id) {
        return null;
    }

    /**
     * 保留
     * @param memberId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult deleteCommentByMemberId(String memberId) {
        return null;
    }
}
